package org.youyun56.gpsserver.command.console;

import java.lang.reflect.Method;

import org.youyun56.gpsserver.command.GlobalProperties;

public class Executor {

    public static final String ALGORITHM_LPR = "LPR"; // least pressure first
    public static final String ALGORITHM_LIR = "LIR"; // last invoked first

    public static final String DISPENSE_STYLE_SINGLE = "SINGLE";
    public static final String DISPENSE_STYLE_ALL = "ALL";

    private String actionClass = null;
    private String method = null;

    @SuppressWarnings("rawtypes")
    public Object execute(ConsoleCommand consoleCommand) throws Exception {
        Class myClass = Class.forName(actionClass, true, Thread.currentThread().getContextClassLoader());
        Object obj = myClass.newInstance();
        Object[] objects = { consoleCommand };
        Class[] classes = { ConsoleCommand.class };
        Method m = obj.getClass().getMethod(method, classes);
        Object result = m.invoke(obj, objects);
        if (result == null) {
            return GlobalProperties.RESULT_FAILED;
        }
        return result;
    }

    public String getActionClass() {
        return actionClass;
    }

    public void setActionClass(String actionClass) {
        this.actionClass = actionClass;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    @Override
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        buffer.append("actionClass:").append(this.actionClass);
        buffer.append(" method:").append(this.method);
        return buffer.toString();
    }
}